﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HyEduOnline.Entities;
using HyEduOnline.Server.Student;
using HyEduOnline.Server.Teacher;
namespace HyEduOnline.Controllers
{
    public class ExamController : Controller
    {

        private StudentsDAO studentsDAO = new StudentsDAO();
        private TestLibPaperDAO testLibPaperDAO = new TestLibPaperDAO();
        private TestLibPaperDetailDAO testLibPaperDetailDAO = new TestLibPaperDetailDAO();
        private TestPaperStrategyDAO testPaperStrategyDAO = new TestPaperStrategyDAO();
        /// <summary>
        /// 等待考试开始页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Wait()
        {
            string stuCode = frontCurrentStu.GetNo();//当前学生学号
            int CourseID = frontCurrentStu.TestState();//考试课程
            //取得当前学生
            TbStudents stu = studentsDAO.findBySNo(stuCode)[0];
            //性别转换
            if (stu.ISex == 1)
                ViewBag.Sex = "女";
            else if (stu.ISex == 0)
                ViewBag.Sex = "";
            else
                ViewBag.Sex = "男";
            //获得当前考试信息
            TbTestLibPaper paper = testLibPaperDAO.GetByID(int.Parse(Session["paper"].ToString()));

            ViewBag.testName = paper.STestName;//取得考试名称

            //计算时间差
            DateTime dt1 = DateTime.Now;
            DateTime dt2 = (DateTime)paper.DTestTime;
            TimeSpan ts = dt2 - dt1;
            
            int seconds = ts.Seconds;
            int minutes = ts.Minutes;
            int hours = ts.Hours;
            //得到倒计时
            ViewBag.waitTime = seconds + minutes * 60 + hours * 3600;

            return View(stu);
        }

        /// <summary>
        /// 考试界面
        /// </summary>
        /// <returns></returns>
        public ActionResult Examing()
        {
            int paperId = int.Parse(Session["paper"].ToString());
           
            //得到当前试卷所有大题（策略）
            IList<TbTestPaperStrategy> strategyList = testPaperStrategyDAO.findByIPaperId(paperId);
            List<TbTestLibPaperDetail> detailList = new List<TbTestLibPaperDetail>(); 
            //得到每个大题（策略）下的小题
            foreach (var item in strategyList)
            {
                List<TbTestLibPaperDetail> temp = testLibPaperDetailDAO.findByIStrategyId(item.Id).ToList();
                detailList.AddRange(temp);
            }
            //当前试卷信息
            TbTestLibPaper paper = testLibPaperDAO.GetByID(paperId);
            List<DateTime> dt = new List<DateTime>();
            dt.Add(DateTime.Now);
            DateTime tempTime = (DateTime)paper.DTestTime;
            tempTime = tempTime.AddMinutes((double)paper.IOverTime);
            dt.Add(tempTime);
            ViewBag.timeList = dt;
            ViewBag.paper = paper;
            ViewBag.strategy = strategyList;
            ViewBag.detail = detailList;
            ViewBag.total = detailList.Count();
            return View();
        }

    }
}
